/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#ifndef __CONF_H__
#define __CONF_H__


#include "common.h"
#include <confuse.h>
#include <vector>


using namespace std;


//-----------------------------------------------------------------------//
//
// File types
//
#define FILE_TYPE_UNKNOWN        0
#define FILE_TYPE_IPACC3         1
#define FILE_TYPE_MASK           0xFFFF
#define FILE_TYPE_GZ             0x10000


//-----------------------------------------------------------------------//
//
// A maximize length name of the traffic class.
//
#define MAX_CLASS_NAME        32


//-----------------------------------------------------------------------//
//
// Condition types.
//
#define CND_TYPE_UNKNOWN     0
#define CND_TYPE_NET         1


//-----------------------------------------------------------------------//
//
// State when condition was cheked.
//
#define EQ_STATE_NEXT   0
#define EQ_STATE_ACCEPT 1
#define EQ_STATE_CANCEL 2


//-----------------------------------------------------------------------//
//
// Conditions testing for the traffic classes.
//
struct ccondition
{


    uint            tc_iid;

    ulong           id;

    ushort          type;

    ulong           net,
                    mask;

    uint            eq_state,
                    not_eq_state;

    ccondition     *eq_next,
                   *not_eq_next;


};


//-----------------------------------------------------------------------//
//
// ...
//
struct ctraffic_class
{


    uint            internal_id;

    char            identifier[ MAX_CLASS_NAME ];

    char            class_name[ MAX_CLASS_NAME ];

    bool            classify_hole;
    bool            process_src;
    bool            process_dst;

    char            next_class_name[ MAX_CLASS_NAME ];
    ctraffic_class *next_class;

    ccondition     *start_condition;


};


//-----------------------------------------------------------------------//
#define  MAX_THREADS             32


//-----------------------------------------------------------------------//
//
// The main configuration file.
//
class cconfig
{


private:

    static  cconfig                   singleton;

    cfg_t                            *cfg;
    vector<ccondition>                vector_condits;
    vector<ctraffic_class>            vector_classes;
    vector<string>                    vector_pre_cmd;
    vector<string>                    vector_post_cmd;

    int                               classes_count;
    ctraffic_class                   *classes;
    int                               condits_count;
    ccondition                       *condits;

    ctraffic_class                  **classes_reordered;


    static char                       file_name[ FILENAME_MAX ];
    static ctraffic_class            *class_traffic_start;
    static ctraffic_class            *classify_filter;

    static int                        files_type;

    static int                        next_cnd_id, prev_cnd_id;

    ctraffic_class  get_or_append_class( const char *name );
    int             parse_config_file( );

    static  uint                      traffic_id_index;

    static int      add_condition( cfg_t *cfg, cfg_opt_t *opt,
                                   int argc, const char **argv );

    static int      out_cmd( cfg_t *cfg, cfg_opt_t *opt,
                             int argc, const char **argv );

    cconfig();
   ~cconfig();


public:

    static const char*     get_file_name();
    static void            set_file_name( const char  *fn );
    static const int       get_files_type();
    static ctraffic_class* get_start_traffic_class();
    static ctraffic_class* get_classify_filter_class();

    static const char*  get_class_identifier( int tc_id );

    static int          parse( );
    static int          release( );

//=========================[ Parsed Values ]=============================//

    // Debug
    static bool         debug_traffic_src_not_remove();

    // Main
    static int          get_hash_size();
    static const char*  get_pid_file();
    static int          get_log_maxsize();
    static const char*  get_log_filename();
    static bool         get_log_debug();

    static int          get_threads_count_min();
    static int          get_threads_count_max();
    static int          get_time_interval_min();
    static int          get_time_interval_max();
    static float        get_distrib_low();

    // Transaction
    static const char*  get_transaction_table();
    static const char*  get_transaction_folder();

    // Source
    static const char*  get_source_folder();
    static const char*  get_files_mask();

    // Out
    static const char*  get_out_folder();
    static const char*  get_out_format_line();
    static const char*  get_out_server();
    static const char*  get_out_stream();
    static const int    get_out_filter_volume_in();
    static const int    get_out_filter_volume_out();
    static vector<string>::const_iterator  get_pre_cmd_begin();
    static vector<string>::const_iterator  get_post_cmd_begin();
    static vector<string>::const_iterator  get_pre_cmd_end();
    static vector<string>::const_iterator  get_post_cmd_end();

    // Arhive
    static const char*  get_arhive_folder();
    static const char*  get_arhive_collect();


//-----------------------------------------------------------------------//
EXCEPTION( e_parse );
EXCEPTION( e_wrong );


};


#endif
